/**
 * 
 */
package com.oet.persistence.operator;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import com.oet.persistence.entity.Category;
import com.oet.persistence.entity.CourseFile;
import com.persistence.template.QueryOperator;

/**
 * @author Zhang Yu
 *
 */
public class CategoryQuery extends QueryOperator<Category> {
	
	public static final String  MULTI_EJBQL=
		"select category from com.oet.persistence.entity.Category category";
	
	public CategoryQuery(EntityManager em)
	{
		setEntityManager(em);
		validate();
		setMaxResults(CategoryQuery.MAX_RESULTS_NUM);
		//Explicitly set the searching rule
		setUserRestrictions(CASE_INSENSITIVE_RESTRICTION_SEARCH);
	}
	
	/* 
	 * This is is used to provide information for Query
	 * Do not change them unless ...
	 */
	@Override
	public String getEntityClassName() {
		// TODO Auto-generated method stub
		return Category.class.getSimpleName();
	}
	
	/* 
	 * This is is used to provide information for Query
	 * Do not change them unless ...
	 */
	@Override
	public String getWholeEntityClassName() {
		// TODO Auto-generated method stub
		//return User.class.getSimpleName();
		return "com.oet.persistence.entity.Category";
	}
	
	/*
	 * Search User with specified field
	 */
	public List<Category> searchCategoryByField(String name, Object value)
	{
		setQueryAttribute(CASE_INSENSITIVE_RESTRICTION_SEARCH_ALL);
		addRestrictionParameter(name, value);
		List<Category> list = getResultList();
		return list;
	}
	
	/*
	 * Search Category with specified field
	 */
	public Category searchCategoryByName(String name)
	{
		setQueryAttribute(CASE_SENSITIVE_RESTRICTION_SEARCH);
		addRestrictionParameter("name", name);
		List<Category> list = getResultList();
		if (list != null && list.size() >= 1)
		{
			return list.get(0);
		}
		return null;
	}
	
	/*
	 * Search Category with specified field
	 */
	public Category searchCategoryByTitle(String title)
	{
		setQueryAttribute(CASE_SENSITIVE_RESTRICTION_SEARCH);
		addRestrictionParameter("name", title);
		List<Category> list = getResultList();
		if (list != null && list.size() >= 1)
		{
			return list.get(0);
		}
		return null;
	}
	
	public List<Category> searchCategoryByMultiField(String name)
	{

		setEjbql(MULTI_EJBQL);
		setQueryAttribute(null);
		setRestrictions(new ArrayList<String>(0));
		
		int i = 0;
		
		if (name != null && !name.isEmpty())
		{
			addRestrictionParameter("category.name", name);
			addRestrictions(" (:Param" + i 
				+ ") like concat('%', concat(lower(:ParamValue" +i 
				+"),'%'))");
			i++;
		}
		
//		if (level != null)
//		{
//			addRestrictionParameter("category.level", level);
//			addRestrictions(" (:Param"+i+") = (:ParamValue" +i +")");
//			i++;
//		}
		
		
		/*
		if (author != null && author.getId() != null)
		{
			addRestrictionParameter("book.author", author);
			addRestrictions(" (:Param"+i+") = (:ParamValue" +i +")");
			i++;
		}
		if (pubisher != null && pubisher.getId() != null)
		{
			addRestrictionParameter("book.publisher", pubisher);
			addRestrictions(" (:Param"+i+") = (:ParamValue" +i +")");
			i++;
		}
		if (category != null && category.getId() != null)
		{
			addRestrictionParameter("book.category", category);
			addRestrictions(" (:Param"+i+") = (:ParamValue" +i +")");
			i++;
		}
		if (bookTag != null && bookTag.getId() != null)
		{
			setEjbql(getEjbql() + BOOK_MULTI_EJBQL_BOOKTAG);
			addRestrictionParameter("booktag", bookTag);
			addRestrictions(" (:Param"+i+") = (:ParamValue" +i +")");
			i++;
		}
		if (partner != null && partner.getId() != null)
		{
			setEjbql(getEjbql() + BOOK_MULTI_EJBQL_TX);
			addRestrictionParameter("tx.partner", partner);
			addRestrictions(" (:Param"+i+") = (:ParamValue" +i +")");
			i++;
		}
		
		if (reader != null && reader.getId() != null)
		{
			if (partner != null && partner.getId() != null)
			{
			}
			else
			{
				setEjbql(getEjbql() + BOOK_MULTI_EJBQL_TX);
			}
			addRestrictionParameter("tx.reader", reader);
			addRestrictions(" (:Param"+i+") = (:ParamValue" +i +")");
			i++;
		}
		*/
		this.setOrderDesc("id");
		
		return getResultList();
	}
	
	
//	public List<CourseFile> searchCourseFileInLibrary(Library lib)
//	{
//		setEjbql(BOOK_MULTI_EJBQL);
//		setQueryAttribute(null);
//		setRestrictions(new ArrayList<String>(0));
//		
//		int i = 0;
//		
//		if (lib != null && lib.getId() != null)
//		{
//			addRestrictionParameter("book.category.library", lib);
//			addRestrictions(" (:Param" + i 
//				+ ") like concat('%', concat(lower(:ParamValue" +i 
//				+"),'%'))");
//			i++;
//		}
//		
//		
////		addRestrictionParameter("book.category.library", lib);
////		addRestrictions(" (:Param" + i 
////				+ ") like concat('%', concat(lower(:ParamValue" +i 
////				+"),'%'))");
////			i++;
//		
//		setOrderDesc("id");
//	
//		return getResultList();
//	}
	
	public List<Category> searchCategoryInList(List<Long> ids)
	{
		setEjbql(MULTI_EJBQL);
		setQueryAttribute(null);
		setRestrictions(new ArrayList<String>(0));
		
		
		addRestrictionParameter("category.id", ids);
		addRestrictions(" (:Param0) in (:ParamValue0)");
		
//		this.setOrderDesc("id");
		setOrderDesc("id");
		return getResultList();
	}
	
	public List<Category> searchCategory(List<Long> ids)
	{
		setEjbql(MULTI_EJBQL);
		setQueryAttribute(null);
		setRestrictions(new ArrayList<String>(0));
		
		addRestrictionParameter("category.id", ids);
		addRestrictions(" (:Param0) in (:ParamValue0)");
		
		setOrderDesc("id");
		return getResultList();
	}
	

}
